using System;
using System.Data;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using System.Collections.Generic;

namespace ASPNETVN.PORTAL.Modules.DOCUMENT.Components
{
    public partial class Item
    {
        public static int IncreaseTotalViews(int itemID)
        {
            const string spName = "p_DOCUMENT_Item_IncreaseTotalViews";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);
            db.AddInParameter(dbCommand, "@ItemID", SqlDbType.Int, itemID);
            return db.ExecuteNonQuery(dbCommand);
        }

        public static int IncreaseTotalDownloads(int itemID)
        {
            const string spName = "p_DOCUMENT_Item_IncreaseTotalDownloads";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);
            db.AddInParameter(dbCommand, "@ItemID", SqlDbType.Int, itemID);
            return db.ExecuteNonQuery(dbCommand);
        }

        public static int IncreaseTotalComments(int itemID)
        {
            const string spName = "p_DOCUMENT_Item_IncreaseTotalComments";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);
            db.AddInParameter(dbCommand, "@ItemID", SqlDbType.Int, itemID);
            return db.ExecuteNonQuery(dbCommand);
        }

        public static DataSet SelectBy_ModuleID(Guid moduleID)
        {
            const string spName = "p_DOCUMENT_Item_SelectBy_ModuleID";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);
            db.AddInParameter(dbCommand, "@ModuleID", SqlDbType.UniqueIdentifier, moduleID);
            return db.ExecuteDataSet(dbCommand);
        }

        public static DataSet SelectBy_ModuleID_CategoryID(Guid moduleID, int categoryID)
        {
            if (categoryID == 0)
            {
                return SelectBy_ModuleID(moduleID);
            }
            const string spName = "p_DOCUMENT_Item_SelectBy_ModuleID_CategoryID";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);
            db.AddInParameter(dbCommand, "@ModuleID", SqlDbType.UniqueIdentifier, moduleID);
            db.AddInParameter(dbCommand, "@CategoryID", SqlDbType.Int, categoryID);

            return db.ExecuteDataSet(dbCommand);
        }

        public static int GetItemCount(Guid moduleID, int categoryID)
        {
            const string spName = "[dbo].[p_DOCUMENT_Item_Count]";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);
            db.AddInParameter(dbCommand, "@ModuleID", SqlDbType.UniqueIdentifier, moduleID);
            db.AddInParameter(dbCommand, "@CategoryID", SqlDbType.Int, categoryID);

            return Convert.ToInt32(db.ExecuteScalar(dbCommand));
        }

        public static DataSet GetTopItems(Guid moduleID, string optionView, int pageSize, int currentPageIndex)
        {
            const string spName = "[dbo].[p_DOCUMENT_Item_SelectAll_ForPaging]";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);
            db.AddInParameter(dbCommand, "@ModuleID", SqlDbType.UniqueIdentifier, moduleID);
            db.AddInParameter(dbCommand, "@OptionView", SqlDbType.VarChar, optionView);
            db.AddInParameter(dbCommand, "@PageSize", SqlDbType.Int, pageSize);
            db.AddInParameter(dbCommand, "@CurrentPageIndex", SqlDbType.Int, currentPageIndex);

            return db.ExecuteDataSet(dbCommand);
        }

        /// <summary>
        /// Get all items by CategoryID
        /// </summary>
        /// <param name="categoryID">CategoryID can be root ID or child ID</param>
        /// <returns>DataSet</returns>
        public static DataSet GetItemsByCategoryID(int categoryID, int pageSize, int currentPageIndex)
        {
            const string spName = "[dbo].[p_DOCUMENT_Item_GetItemsBy_CategoryID]";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);
            db.AddInParameter(dbCommand, "@CategoryID", SqlDbType.Int, categoryID);
            db.AddInParameter(dbCommand, "@PageSize", SqlDbType.Int, pageSize);
            db.AddInParameter(dbCommand, "@CurrentPageIndex", SqlDbType.Int, currentPageIndex);

            return db.ExecuteDataSet(dbCommand);
        }

        public static DataSet GetInformation(int id)
        {
            const string spName = "[dbo].[p_DOCUMENT_Item_GetInformation]";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@ID", SqlDbType.Int, id);

            return db.ExecuteDataSet(dbCommand);
        }

        public static int GetItemID_ByCode(string code)
        {
            const string commandText = "SELECT TOP 1 ID FROM t_DOCUMENT_Items WHERE Code = @Code";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetSqlStringCommand(commandText);

            db.AddInParameter(dbCommand, "@Code", SqlDbType.NVarChar, code);

            return (int) db.ExecuteScalar(dbCommand);
        }

        public static DataSet Search(Guid moduleID, string whereCondition)
        {
            const string spName = "[dbo].[p_DOCUMENT_Item_Search]";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@ModuleID", SqlDbType.UniqueIdentifier, moduleID);
            db.AddInParameter(dbCommand, "@WhereCondition", SqlDbType.NVarChar, whereCondition);

            return db.ExecuteDataSet(dbCommand);
        }
    }
}